﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Icst.Leaflike.Data;
using Icst.Leaflike.Logging;

namespace Icst.Leaflike.Web.admin
{
    public partial class cache : System.Web.UI.Page
    {
        class CacheAction
        {
            public CacheAction() { }
            public CacheAction(string name, Action action) { Name = name; Action = action; }
            public string Name { get; set; }
            public Action Action { get; set; }
        }
        private static List<CacheAction> _actions = new List<CacheAction>()
        {
            new CacheAction("分类", ()=>{ Category.Instance.Refresh();}),
            new CacheAction("基础树/定义", ()=>{Tree.Instance.Refresh();}),
            new CacheAction("系统参数", ()=>{ SysSetting.Instance.Refresh();}),
            new CacheAction("推荐书籍", ()=>{ Book.Instance.Refresh();}),
            new CacheAction("我的收集", ()=>{ Gathering.Instance.Refresh();}),
        };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grid.DataSource = _actions;
                grid.DataBind();
            }
        }
        protected void refreshAll_Click(object sender, EventArgs e)
        {
            try
            {
                Logger.Warning("刷新所有");

                foreach (CacheAction p in _actions)
                {
                    Logger.Warning("正在刷新 " + p.Name);
                    Action action = p.Action;
                    action.Invoke();
                }

                string warningScript = "alert('刷新所有成功!')";
                ScriptManager.RegisterClientScriptBlock(updatePanel, updatePanel.GetType(), "", warningScript, true);
            }
            catch (Exception ex)
            {
                string warningScript = "alert('警告：刷新失败!')";
                ScriptManager.RegisterClientScriptBlock(updatePanel, updatePanel.GetType(), "", warningScript, true);
                Logger.Warning("刷新失败. " + ex.Message);
            }
        }

        protected void refresh_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            string name = b.CommandArgument;
            try
            {
                Logger.Warning("正在刷新 " + name);

                Action action = _actions.SingleOrDefault(p => p.Name == name).Action;
                action.Invoke();

                Logger.Warning("刷新成功 " + name);

                string warningScript = "alert('刷新成功!')";
                ScriptManager.RegisterClientScriptBlock(updatePanel, updatePanel.GetType(), "", warningScript, true);
            }
            catch (Exception ex)
            {
                string warningScript = "alert('警告：刷新失败!')";
                ScriptManager.RegisterClientScriptBlock(updatePanel, updatePanel.GetType(), "", warningScript, true);
                Logger.Warning("刷新失败 " + name + ". " + ex.Message);
            }
        }
    }
}
